<?php

/**
 * 管理后台日志行为
 *
 * @author birdy <freebirdy@foxmail.com>
 * @link http://www.aiyuangong.com
 */

namespace app\admin\behavior;

use think\Exception;
use think\Session;
use think\Config;
use think\Request;

class WebLog
{
    public function run(&$param)
    {
        // 屏蔽异常
        try {
            $request = Request::instance();
            $requestData = $request->param();
            // 截取一部分数据，避免数据太大导致存储出错，比如文章发布提交的数据
            foreach ($requestData as &$v) {
                if (is_string($v)) {
                    $v = mb_substr($v, 0, 200);
                }
            }

            $data = [
                'uid' => Session::get(Config::get('rbac.user_auth_key')) ?: 0,
                'ip' => $request->ip(),
                'location' => implode(' ', \Ip::find($request->ip())),
                'os' => \Agent::getOs(),
                'browser' => \Agent::getBroswer(),
                'url' => $request->url(),
                'module' => $request->module(),
                'map' => $request->controller() . "/" . $request->action(),
                'is_ajax' => $request->isAjax() ? 1 : 0,
                'data' => serialize($requestData),
                'otime' => time(),
            ];

            \WebLog::instance()->record($data);
        } catch (Exception $e) {
            //to do write exception log
        }
    }

}
